from rest_framework.serializers import ModelSerializer

from courses.models import CourseLesson, CourseChapter, Course
from order import models
from rest_framework import serializers


class OrderSerializer(ModelSerializer):
    """
    订单序列化器
    """
    class Meta:
        model = models.Order
        fields = "__all__"


class OrderDetailSerializer(ModelSerializer):
    """
    订单详情序列化器
    """
    class Meta:
        model = models.OrderDetail
        fields = "__all__"


class OrderDetailSer(ModelSerializer):
    course = serializers.SerializerMethodField()

    def get_course(self, obj):
        return {"course_name": obj.course.name, "brief": obj.course.brief, "cover": obj.course.course_img,
                "teacher_name": obj.course.teacher.name}

    class Meta:
        model = models.OrderDetail
        fields = "__all__"


class OrderInfoSerializer(ModelSerializer):
    """
    订单信息序列化器
    """
    orderdetail_set = OrderDetailSer(many=True)

    class Meta:
        model = models.Order
        fields = "__all__"


class LessonSerializer(ModelSerializer):
    """
    课时序列化器
    """
    class Meta:
        model = CourseLesson
        fields = "__all__"


class ChapterSerializer(ModelSerializer):
    """
    章节序列化器
    """
    lesson = LessonSerializer(many=True)

    class Meta:
        model = CourseChapter
        fields = "__all__"


class CourseDetailSerializer(ModelSerializer):
    """
    课程详情序列化器
    """
    chapters = ChapterSerializer(many=True)

    class Meta:
        model = Course
        fields = "__all__"


class OrderCourseSerializer(ModelSerializer):
    """
    订单课程序列化器
    """
    course = CourseDetailSerializer()

    class Meta:
        model = models.OrderDetail
        fields = "__all__"
